﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClientDBAgent;


namespace HF.Business.Common
{
    /// <summary>
    /// AffixFiles 的摘要说明
    /// </summary>
    public class AffixFiles
    {
        /// <summary>
        /// 附件Id
        /// </summary>
        public string AffixId;
        public string WorkflowId = "";
        public string WorkflowInsId = "";
        public string WorktaskId = "";

        /// <summary>
        /// 附件任务实例Id
        /// </summary>
        public string WorktaskInsId;
        /// <summary>
        /// 文件Id
        /// </summary>
        public string FileNameId;
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName;
        /// <summary>
        /// 文件存放相对路径
        /// </summary>
        public string FilePath;


        //工作流模板WorkFlowClass表的 访问参数
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList ={ "AffixId", "WorkflowId", "WorkflowInsId", "WorktaskId", "WorktaskInsId",
                                "FileNameId","FileName","FilePath"};
        private string tableName = "Comm_AffixFiles";
        private string keyField = "AffixId";
        private string sqlString = "";

        public AffixFiles()
        {

        }
        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="workflowClassId">附件Id</param>
        /// <returns></returns>
        public static int DeleteAffix(string affixId)
        {
            try
            {
                string sqlStr = "delete Comm_AffixFiles where AffixId=@affixId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@affixId", affixId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点附件列表
        /// </summary>
        /// <param name="FatherClassId">流程实例Id</param>
        /// <returns>附件列表table</returns>
        public static DataTable GetAffixTable(string workflowInsId,bool satue)
        {
            try
            {
                string tmpStr = string.Empty;
                if (satue)
                {
                    //string tmpStr = "select * from Comm_AffixFiles where  workflowInsId=@workflowInsId";
                    //tmpStr = "select A.*,W.Status,W.TaskInstanceCaption from Comm_AffixFiles A inner join HF_WorkTaskInstance W " +
                    //             " on A.WorktaskId = W.WorkTaskId where A.WorkflowInsId = @workflowInsId";

                    tmpStr = "select A.*,W.Status,W.TaskInstanceCaption from  " +
                            " (select * from Comm_AffixFiles where WorkflowInsId = @workflowInsId) as A inner join " +
                            " (select * from HF_WorkTaskInstance W where W.WorkFlowInstanceId = @workflowInsId) as W " +
                            " on A.WorktaskId = W.WorkTaskId";
                }
                else 
                {
                    tmpStr = "select A.*, 1 as Status,W.TaskCaption as TaskInstanceCaption from Comm_AffixFiles A inner join HF_WorkTask W on "+
                            " A.WorktaskId = W.WorkTaskId where workflowInsId= @workflowInsId";
                }
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workflowInsId", workflowInsId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        private void setParameter()
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@AffixId", AffixId);
            sqlDataItem.AppendParameter("@WorkflowId", WorkflowId);
            sqlDataItem.AppendParameter("@WorkflowInsId", WorkflowInsId);
            sqlDataItem.AppendParameter("@WorktaskId", WorktaskId);
            sqlDataItem.AppendParameter("@WorktaskInsId", WorktaskInsId);
            sqlDataItem.AppendParameter("@FileNameId", FileNameId);
            sqlDataItem.AppendParameter("@FileName", FileName);
            sqlDataItem.AppendParameter("@FilePath", FilePath);
        }
        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ",";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";

            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }
        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = this.fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + "=@" + tmpFieldName + ",";

            }
            tmpFieldName = fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + "=@" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "='" + this.AffixId + "'";
            sqlDataItem.CommandText = sqlString;
        }

        /// <summary>
        /// 新建 附件
        /// </summary>
        public void InsertAffix(AffixFiles entity)
        {
            if (entity.AffixId.Trim().Length == 0 || entity.AffixId == null)
                throw new Exception("InsertAffix方法错误，AffixId 不能为空！");

            try
            {
                setInsertSql();//设定insert语句
                setParameter();//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 修改一个附件
        /// </summary>
        public void UpdateAffix()
        {
            if (AffixId.Trim().Length == 0 || AffixId == null)
                throw new Exception("UpdateAffix方法错误，AffixId 不能为空！");

            try
            {
                setUpdateSql();//设定定Update语句
                setParameter();//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public void InsertAffixcrm(AffixFiles entity)
        {
            if (entity.AffixId.Trim().Length == 0 || entity.AffixId == null)
                throw new Exception("InsertAffix方法错误，AffixId 不能为空！");

            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void setParameter(AffixFiles entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@AffixId", entity.AffixId);
            sqlDataItem.AppendParameter("@WorkflowId", entity.WorkflowId);
            sqlDataItem.AppendParameter("@WorkflowInsId", entity.WorkflowInsId);
            sqlDataItem.AppendParameter("@WorktaskId", entity.WorktaskId);
            sqlDataItem.AppendParameter("@WorktaskInsId", entity.WorktaskInsId);
            sqlDataItem.AppendParameter("@FileNameId", entity.FileNameId);
            sqlDataItem.AppendParameter("@FileName", entity.FileName);
            sqlDataItem.AppendParameter("@FilePath", entity.FilePath);
        }
    }
}
